package edu.bu.cs673.superkids123.common.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.swing.ImageIcon;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.parents.model.Image;

@Entity
@Table(name = "achievements")
@PrimaryKeyJoinColumn(name="AchievementsID")
public class Achievement {
	
	private static final int DEFAULT_POINTS = 100;
	private static final float IMAGE_SCALE = 0.70f;
	private static final int GOLD_MIN = 200;
	private static final int SILVER_MIN = 100;
	
	@Id
	@GeneratedValue
	@Column(name = "AchievementsID")
	private int id;

	@Column(name = "AchievementsName")
	private String name;

	@Column(name = "AchievementsDescription")
	private String description;

	@Column(name = "AchievementsPrize")
	private String prize;

	@Column(name = "AchievementsPoints")
	private int points;
	
	@Column(name = "AchievementsActive")
	private boolean active;
	
	@ManyToOne
	@Cascade({CascadeType.SAVE_UPDATE})
	@NotFound(action=NotFoundAction.IGNORE)
	@JoinColumn(name="ImageID")
	private Image image = null;
	
	@OneToMany(mappedBy="achievement",fetch=FetchType.EAGER,orphanRemoval=true)
	@Cascade({CascadeType.ALL})
	@NotFound(action=NotFoundAction.IGNORE)
	private Set<AchievementRule> rules = new HashSet<AchievementRule>();

	// Public Interface
	
	public Achievement() {
		this.points = DEFAULT_POINTS;
		this.active = true;
	}
	
	public Achievement(String name, String prize, String description) {

		this.name = name;
		this.description = description;
		this.prize = prize;
		this.points = DEFAULT_POINTS;
		this.active = true;
	}
	
	/** 
	 * Used for small icons
	 * 
	 * @return ImageIcon
	 */
	public ImageIcon getMedalIcon() {
		
		ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
		ImageIcon icon = null;
		
		if (points >= GOLD_MIN) 
			icon = provider.getImage(ChildrenResource.iconGoldMedal);
		else if (points >= SILVER_MIN) 
			icon = provider.getImage(ChildrenResource.iconSilverMedal);
		else
			icon = provider.getImage(ChildrenResource.iconBronzeMedal);
		
		return icon;
	}

	/**
	 * Used for larger images
	 *  
	 * @return ImageIcon
	 */
	public ImageIcon getMedalImage() {
		
		ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
		ImageIcon image = null;
		
		if (points >= GOLD_MIN) 
			image = provider.getScaledImage(ChildrenResource.imageGoldMedal, IMAGE_SCALE);
		else if (points >= SILVER_MIN) 
			image = provider.getScaledImage(ChildrenResource.imageSilverMedal, IMAGE_SCALE);
		else
			image = provider.getScaledImage(ChildrenResource.imageBronzeMedal, IMAGE_SCALE);
		
		return image;
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if (obj instanceof Achievement) {
			if (((Achievement) obj).getId() == this.getId())
				return true;
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		return id;
	}
	
	// Private Interface
	
	
	// Setters & Getters
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPrize() {
		return prize;
	}

	public void setPrize(String prize) {
		this.prize = prize;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public Set<AchievementRule> getRules() {
		return rules;
	}

	public void setRules(Set<AchievementRule> rules) {
		this.rules = rules;
	}
}
